package cn.sxt.oop;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * 测试二分法（折半查找）
 */
public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {30, 20, 50, 10, 80, 9, 7, 12, 100, 40, 8};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        int i = myBinarySearch(arr, 25);
        System.out.println(i);
    }

    public static int myBinarySearch(int[] arr, int value){
        int low = 0;
        int high = arr.length-1;
        while (low<=high){
            int mid = (low + high)>>>1;
            if (value == arr[mid]){
                return mid;
            }
            if (value > arr[mid]){
                low = mid+1;
            }
            if(value < arr[mid]){
                high = mid-1;
            }
        }
        //没找到返回-1
        return -1;
    }
}
